/* ====================================================================
 * (#)Header: JmsMessenger.java
 * (#)Revision: 
 * (#)Date Created: 02-Nov-09 10:32:15 AM
 * (#)Created By: Karol Wozniak
 * ====================================================================
 *
 * Copyright (c) 2007 karolak .  All rights reserved.
 *
 * ====================================================================
 * 
 * The software application represented here is the sole property of 
 * karolak. Its unauthorized utilization is expressly prohibited.
 * 
 * ====================================================================
 */
package com.karolak.jms;

import java.io.Serializable;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;

/**
 * <code>JmsMessenger</code> is used to ....
 * 
 * @author <a href="mailto:kwozniak@hotmail.com">Karol Wozniak</a>
 */
public class JmsMessenger {

	private JmsSender sender;
	private JmsReceiver receiver;

	public JmsMessenger(String destinationJndiNameOut, String destinationJndiNameIn) {
		JmsSender sender = new JmsSender(destinationJndiNameOut);
		Connection connection = sender.getConnection();
		JmsReceiver receiver = new JmsReceiver(connection, destinationJndiNameIn);
		setSender(sender);
		setReceiver(receiver);
	}

	public void sendMessage(String text) throws JMSException {
		getSender().sendMessage(text);
	}

	public void sendMessage(Serializable object) throws JMSException {
		getSender().sendMessage(object);
	}

	public Message receiveMessage() throws JMSException {
		return getReceiver().receiveMessage();
	}

	public void start() {
		getSender().start();
		getReceiver().start();
	}

	public void stop() {
		getSender().stop();
		getReceiver().stop();
	}

	public void close() {
		getSender().close();
		getReceiver().close();
	}

	public void setMessageListener(MessageListener messageListener) {
		getReceiver().setMessageListener(messageListener);
	}

	// === get/set === //
	public JmsSender getSender() {
		return sender;
	}

	public void setSender(JmsSender sender) {
		this.sender = sender;
	}

	public JmsReceiver getReceiver() {
		return receiver;
	}

	public void setReceiver(JmsReceiver receiver) {
		this.receiver = receiver;
	}
}
